package lovoStudy_9_7.StudentManage;

/**
 * Created with Intellij IDEA
 * Description:
 *
 * @ return:
 * @ author:zxl
 */

import javax.swing.*;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 学生管理系统主界面类
 * */
public class StudentUI {
    public static Scanner scanner = new Scanner(System.in);

    public SuperArray superArray = new SuperArray();


    /**
     * 学生管理系统主程序
     */
    public void UIShow() {
        JOptionPane.showMessageDialog(null,"欢迎来到学生管理系统!");

        while(true){
            try{
                menu();
                break;
            }catch (NullPointerException e){
                JOptionPane.showMessageDialog(null,"程序退出");
                System.exit(0);
            }


            }
        }


    /**
     * 菜单
     */
    public void menu() {
        while (true) {
            String input = JOptionPane.showInputDialog(null,"请输入你的选择: 1-增加学生 " +
                    "2-删除学生 3-修改学生 4-查询学生 5-退出程序");

            if (input.equals("1")) {
                add();
            } else if (input.equals("2")) {
                remove();
            } else if (input.equals("3")) {
                change();
            } else if (input.equals("4")) {
                get();
            } else if (input.equals("5")){
                JOptionPane.showMessageDialog(null,"程序退出! 再见");
                System.exit(0);
            } else {
                JOptionPane.showMessageDialog(null,"输入有误!");
            }

        }
    }


    /**
     * 增加学生
     */

    public void add() {
        String name = JOptionPane.showInputDialog(null,"请输入学生名字：");
        String age = JOptionPane.showInputDialog(null,"请输入学生年龄：");
        char [] str = age.toCharArray();
        for (int i = 0; i< str.length;i++){
            if (str[i] < 48 || str[i] > 57){
                JOptionPane.showMessageDialog(null,"年龄请输入数字");
                return;
            }
        }
        String gender = JOptionPane.showInputDialog(null,"请输入学生性别：");
        int confirm = JOptionPane.showConfirmDialog(null,"确认添加?");
        if (confirm == 0){
            superArray.add(new Student(name, age, gender));
            JOptionPane.showMessageDialog(null,"增加成功");
        }
    }

    /**
     * 减少学生
     */
    public void remove() {
        String name = JOptionPane.showInputDialog(null,"请输入学生的名字: ");
        for (int i = 0; i < superArray.size(); i++) {
            if (superArray.get(i).equals(new Student(name))) {
                int confirm = JOptionPane.showConfirmDialog(null,"确认删除?");
                if (confirm == 0){
                    superArray.remove(i);
                    JOptionPane.showMessageDialog(null,"删除成功");
                    return;
                }
            }
        }
        JOptionPane.showMessageDialog(null,"该学生不存在");
    }

    /**
     * 修改学生
     */
    public void change() {
        String name = JOptionPane.showInputDialog(null,"请输入学生的名字: ");
        for (int i = 0; i < superArray.size(); i++) {
            Student student = (Student) superArray.get(i);
            if (superArray.get(i).equals(new Student(name))) {
                String input = JOptionPane.showInputDialog(null,"请选择 1-姓名 2-年龄 3-性别");
                if (input.equals("1")){
                    String newName = JOptionPane.showInputDialog(null,"请输入该学生的姓名: ");
                    int confirm = JOptionPane.showConfirmDialog(null,"确认修改?");
                    if (confirm == 0){
                        student.setName(newName);
                        JOptionPane.showMessageDialog(null,"修改成功");
                        return;
                    }
                }else if (input.equals("2")){
                    String newAge = JOptionPane.showInputDialog(null,"请输入该学生的年龄: ");
                    int confirm = JOptionPane.showConfirmDialog(null,"确认修改?");
                if (confirm == 0){
                student.setAge(newAge);
                JOptionPane.showMessageDialog(null,"修改成功");
                return;
                    }

                }else if (input.equals("3")){
                    String newGender = JOptionPane.showInputDialog(null,"请输入该学生的性别: ");
                    int confirm = JOptionPane.showConfirmDialog(null,"确认修改?");
                    if (confirm == 0){
                        student.setGender(newGender);
                        JOptionPane.showMessageDialog(null,"修改成功");
                        return;
                    }
                }else {
                    JOptionPane.showMessageDialog(null,"输入有误");
                    return;
                }

            }
        }

        JOptionPane.showMessageDialog(null,"查无此人");
    }

    /**
     * 查询学生
     */
    public void get() {
        String input = JOptionPane.showInputDialog(null,"请选择 1-全部学生 2-学生姓名");
        if (input .equals("1")){
            for (int i = 0; i < superArray.size(); i++){
                JOptionPane.showMessageDialog(null,superArray.get(i));
            }
        }else if (input.equals("2")){
            String name = JOptionPane.showInputDialog(null,"请输入学生姓名: ");
            for (int i = 0; i < superArray.size(); i++) {
                if (superArray.get(i).equals(new Student(name))) {
                    System.out.println(superArray.get(i));
                    return;
                }
            }
            JOptionPane.showMessageDialog(null,"该学生不存在");
        }else {
            JOptionPane.showMessageDialog(null,"输入有误");
        }


    }
}